package com.googlecode.monitoring.ws.dao;


import com.googlecode.monitoring.dao.AgentConfigurationDao;
import com.googlecode.monitoring.dao.XmlUtils;
import com.googlecode.monitoring.exception.DataDaoException;
import com.googlecode.monitoring.model.AlertText;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.model.Person;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Repository;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Repository
public class XmlWsAgentConfigurationDao extends AgentConfigurationDao {

    private Resource xmlWsConfigResource;

    Logger log = Logger.getLogger(XmlWsAgentConfigurationDao.class);

    private XmlUtils xmlUtils;
    private WsAgentXmlUtils wsAgentXmlUtils;

    @Autowired
    public void setWsAgentXmlUtils(WsAgentXmlUtils wsAgentXmlUtils) {
        this.wsAgentXmlUtils = wsAgentXmlUtils;
    }

    @Autowired
    public void setXmlUtils(XmlUtils xmlUtils) {
        this.xmlUtils = xmlUtils;
    }

    public Resource getXmlWsConfigResource() {
        return xmlWsConfigResource;
    }
    
    @Autowired
    public void setXmlWsConfigResource(Resource xmlWsConfigResource) {
        this.xmlWsConfigResource = xmlWsConfigResource;
    }

    /**
     * Gets all WsItemToCheck objects from XML file.
     * Sets persons from <alerts> tag as responsible persons list of WsItemToCheck if there is no specific persons
     *
     * @return List of WsItemToCheck objects
     */
    @Override
    public List<ItemToCheck> getAllItemsFromConfiguration() {
        return getXmlDataItems(xmlWsConfigResource);
    }

    private List<ItemToCheck> getXmlDataItems(Resource xmlResource) throws DataDaoException {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlResource);

            NodeList alertPersonsNodeList = ((Element) doc.getElementsByTagName("alerts").item(0)).getElementsByTagName("person");
            Set<Person> alertPersons = xmlUtils.buildPersons(alertPersonsNodeList);

            NodeList nodes = doc.getElementsByTagName("item");
            List<ItemToCheck> items = new ArrayList<ItemToCheck>();
            log.debug("read " + nodes.getLength() + " nodes item");
            for (int i = 0; i < nodes.getLength(); i++) {
                Node xmlItem = nodes.item(i);
                if (xmlItem.getNodeType() == Node.ELEMENT_NODE) {
                    items.add(wsAgentXmlUtils.buildInstanceToCheck((Element) xmlItem, alertPersons));
                }
            }
            return items;
        } catch (Throwable e) {
            throw new DataDaoException(e);
        }
    }

    /**
     * reading xml document and build AlertText object from xml node
     *
     * @return AlertText
     * @throws DataDaoException
     */
    @Override
    public AlertText getAlertText() throws DataDaoException {
        try {
            Document doc = xmlUtils.getDocumentFromResource(xmlWsConfigResource, false);
            return xmlUtils.getAlertText(doc);
        } catch (Throwable e) {
            log.error(e);
            throw new DataDaoException(e);
        }
    }

    @Override
    public List<Person> getCommonResponsiblePersons() {
        return null;
    }
}
